#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;


#define MAX_N 500005

struct lnode
{
    int cnt;
    int self;
    int val;
    int lch,rch;
} tree[MAX_N];
int tree_cnt;

int a[MAX_N],sorted[MAX_N];

int mktree(int l,int r)
{
    if (l>r) return -1;
    int m = (l+r) >> 1;
    int lch = mktree(l,m-1);
    int rch = mktree(m+1,r);
    tree_cnt++;
    tree[tree_cnt].lch = lch;
    tree[tree_cnt].rch = rch;
    tree[tree_cnt].val = sorted[m];
    return tree_cnt;
}

int Insert(int p  , int root, long long &big)
{
    if (p < tree[root].val)
    {
        big += tree[root].cnt + tree[root].self;
        Insert(p,tree[root].lch,big);
    }
    if (p>tree[root].val)
    {
        tree[root].cnt ++;
        Insert(p , tree[root].rch,big);
    }
    if (p==tree[root].val)
    {
        tree[root].self++;
        big+= tree[root].cnt;
    }
}

int main()
{
  //  freopen("2299.txt","r",stdin);
    int n;
    while (scanf("%d",&n)!=EOF && n)
    {
        for (int i = 0 ; i < n ; i++)
        {
            scanf("%d",&a[i]);
            sorted[i] = a[i];
        }
        sort(sorted,sorted+n);
        tree_cnt = 0;
        memset(tree,0,sizeof(tree));
        int j = 0 ;
        for (int i = 1; i < n ; i++)
        {
            if (sorted[i] != sorted[j])
            {
                j++;
                sorted[j] = sorted[i];
            }
        }
        int root = mktree(0,j);



        long long ans = 0;
        long long big;
        for (int i= 0 ; i < n ; i++ )
        {
            big = 0;
            Insert(a[i],root,big);
            ans += big;
        }
        cout << ans<<endl;
    }

    return 0;
}
